package com.example.governmentcrawler.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.governmentcrawler.constant.KeyWordsConst;
import com.example.governmentcrawler.constant.UrlConst;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @projectName: GovernmentCrawler
 * @package: com.example.governmentcrawler.utils
 * @className: GetRealUrls
 * @author: xxXPCSC
 * @description: TODO
 * @date: 2024/2/29 14:41
 * @version: 1.0
 **/
@Component
public class GetRealUrls {
    @Autowired
    private SendHttp sendHttp;
    @Autowired
    private FileSup fileSup;

    //处理第一个网站
    public Map<String,Map> getRealUrlsForGov(Map<String,Map> urls){
        Map<String,Map> result = new HashMap<>();
        //键为title 值为url
        for(String outkey: urls.keySet()) {
            LinkedHashMap<String, String> tempMap = new LinkedHashMap<>();
            Map<String, String> innerMap = urls.get(outkey);
            //不直接使用 keyset()因为对map集合做了修改会导致null指针异常
            for (String title : innerMap.keySet()) {
                int startIndex =innerMap.get(title).indexOf("articleId=");
                String temp = innerMap.get(title).substring(startIndex + "articleId=".length());

                //转换为url编码
                String encode = URLEncoder.encode(temp);
                String s = "articleId=" + encode;
                if(outkey.equals("采购项目公告")) {
                    JSONObject jsonObject = sendPostForFov(UrlConst.GovDetailPrefixUrl + s, s, "application/json");
                    if(jsonObject==null){
                        System.out.println("Gov接口连接失败，默认符合要求");
                        tempMap.put(title, innerMap.get(title));
                        continue;
                    }
                    if (getContent(jsonObject)) {
                        //不能用简单的删除会导致null异常
                        //urls.get(outkey).remove(title);
                        tempMap.put(title, innerMap.get(title));
                    }
                }
                else if(outkey.equals("采购意向")){
                    JSONObject jsonObject = sendPostForFov(UrlConst.GovDetailPrefixUrl + s, s, "application/json");
                    if(jsonObject==null){
                        System.out.println("Gov接口连接失败，默认符合要求");
                        //返回为false说明title有后缀
                        if(!lastExist(title))
                            tempMap.put(title, innerMap.get(title));
                        continue;
                    }
                    String s_later = getContentForIntention(jsonObject);
                    if (s_later!=null) {
                        int lastIndex = title.lastIndexOf('[');
                        String new_title = title.substring(0, lastIndex)+"["+s_later+"]";
                        //不能用简单的删除会导致null异常
                        //urls.get(outkey).remove(title);
                        //将原来的键更改为新的键
                        tempMap.put(new_title, innerMap.get(title));
                    }

                }
            }
            if(outkey.equals("采购意向")){
                //如果为空，或者状态为为0 则重新写入
                if(fileSup.ReadForFile()==null||fileSup.ReadForState()==0){
                    //如果写入成功 状态位为1 否则状态为为0
                    if(fileSup.WritInFile(tempMap)){
                        fileSup.WritInState(1);
                    }else{
                        fileSup.WritInState(0);
                    }
                }else{//如果不为空且状态为不为0的话 读取上一次发送的邮件数据
                    tempMap.putAll(filterExpireData(fileSup.ReadForFile()));
                    //重新写入最开始的 且状态位为1
                    if(fileSup.WritInFile(tempMap)){
                        fileSup.WritInState(1);
                    }else{
                        fileSup.WritInState(0);
                    }
                }
            }
            result.put(outkey,tempMap);
        }
        return result;
    }

    //处理第二个第三个网站 content infoContentM
    public Map<String,Map> getRealUrlsForTwoAndThree(Map<String,Map> urls){
        Map<String,Map> result = new HashMap<>();
        for (String outkey: urls.keySet()) {
            LinkedHashMap<String, String> tempMap = new LinkedHashMap<>();
            Map<String,String> innerMap = urls.get(outkey);
            for (String title : innerMap.keySet()) {
                String s = innerMap.get(title);
                String s1 = sendPostForTwoAndThree(s);
                if(s1==null){
                    System.out.println("网站接口连接失败，默认符合要求");
                    tempMap.put(title,s);
                    continue;
                }
                if(getContentForTwoAndThree(s1)){
                    tempMap.put(title,s);
                }
            }
            result.put(outkey,tempMap);
        }
        return result;
    }

    //处理第四个网站 class="ewb-article-info"
    public Map<String,Map> getRealUrlsForPublic(Map<String,Map> urls){
        Map<String,Map> result = new HashMap<>();
        for (String outkey: urls.keySet()) {
            Map<String,String> innerMap = urls.get(outkey);
            LinkedHashMap<String, String> tempMap = new LinkedHashMap<>();
            for (String title : innerMap.keySet()) {
                String s = innerMap.get(title);
                String s1 = sendPostForPublic(s);
                if(s1==null){
                    System.out.println("Public接口连接失败,默认符合要求");
                    tempMap.put(title,s);
                    continue;
                }
                if (getContentForFour(s1)) {
                    tempMap.put(title,s);
                }
            }
            result.put(outkey,tempMap);
        }
        return result;
    }

    //处理第五个网站 class=layui-row layui-col-space15
    public Map<String,String> getRealUrlsForOnline(Map<String,String> urls){
        Map<String,String> result = new LinkedHashMap<>();
        for(String title:urls.keySet()){
            String s = urls.get(title);
            String s1 = sendPostForOnline(s);
            if(s1==null){
                System.out.println("Online接口连接失败,默认符合要求");
                result.put(title,s);
                continue;
            }
            if(getContentForFive(sendHttp.sendHTTPPostForTwoAfter(s))){
               result.put(title,s);
            }
        }
        return result;
    }

    private JSONObject sendPostForFov(String url, String parms,String header){
        try{
            return sendHttp.sendHTTPPost(url, parms, header);
        }catch (Exception e){
            return null;
        }

    }

    private String sendPostForTwoAndThree(String s){
        try{
            return sendHttp.sendHTTPPostForTwoAfter(s);
        }catch (Exception e){
            return null;
        }
    }

    private String sendPostForPublic(String s){
        try{
            return sendHttp.sendHTTPPostForTwoAfter(s);
        }catch (Exception e){
            return null;
        }
    }

    private String sendPostForOnline(String s){
        try{
            return sendHttp.sendHTTPPostForTwoAfter(s);
        }catch (Exception e){
            return null;
        }
    }

    //第一个网站处理字符串得到字符串内容
    public Boolean getContent(JSONObject jsonObject){
        if(jsonObject.getBoolean("success")){
            String string = jsonObject.getJSONObject("result").getJSONObject("data").getString("content");
            for(int i=0;i< KeyWordsConst.MyKeyWord.length;i++){
                if(string.contains(KeyWordsConst.MyKeyWord[i])){
                    return true;
                }
            }

        }
        return false;
    }
    //处理政府采购意向
    public String getContentForIntention(JSONObject jsonObject){
        StringBuilder stringBuilder = new StringBuilder();
        if(jsonObject.getBoolean("success")){
            String string = jsonObject.getJSONObject("result").getJSONObject("data").getString("content");
            Document parse = Jsoup.parse(string);
            //projectname
            Element  projectname= parse.select("tr").get(1);
            Element projectneed  = parse.select("tr").get(3);
            string=projectname.text()+projectneed.text();
            for(int i=0;i< KeyWordsConst.MyKeyWord.length;i++){
                if(string.contains(KeyWordsConst.MyKeyWord[i])){
                    stringBuilder.append(KeyWordsConst.MyKeyWord[i]+"|");
                }
            }
            String next = stringBuilder.toString();
            if (next.endsWith("|")) {
                next = next.substring(0, next.length() - 1);
                return next;
            }
        }

        return null;
    }

    //第二和三个网站处理html源码
    public Boolean getContentForTwoAndThree(String html){
        if(html==null)
            //为空说明接口连接失败 默认不需要处理
            return true;
        Document doc = Jsoup.parse(html);
        Element contentDiv = doc.select(".content").first();
        Element infoContentDiv = contentDiv.select("#infoContentM").first();
        Elements pTags = infoContentDiv.select("p");
        StringBuffer stringBuffer = new StringBuffer();
        for (Element pTag : pTags) {
            stringBuffer.append(pTag.text());
        }
        String s = stringBuffer.toString();
        for(int i=0;i<KeyWordsConst.MyKeyWord.length;i++){
            if(s.contains(KeyWordsConst.MyKeyWord[i]))
                return true;
        }
        return false;
    }
    //第四个网站处理html源码
    public Boolean getContentForFour(String html){
        Document doc = Jsoup.parse(html);
        Element contentDiv = doc.select(".ewb-article-info").first();
        Elements pTags = contentDiv .select("p");

        StringBuffer stringBuffer = new StringBuffer();

        for (Element pTag : pTags) {
            stringBuffer.append(pTag.text());
        }
        String s = stringBuffer.toString();
        for(int i=0;i<KeyWordsConst.MyKeyWord.length;i++){
            if(s.contains(KeyWordsConst.MyKeyWord[i]))
                return true;
        }
        return false;
    }
    //第五个网站处理html源码
    public Boolean getContentForFive(String html){
        Document doc = Jsoup.parse(html);
        Element contentDiv = doc.select(".layui-row layui-col-space15").first();
        Elements pTags = contentDiv.select("div");
        StringBuffer stringBuffer = new StringBuffer();
        for (Element pTag : pTags) {
            stringBuffer.append(pTag.text());
        }
        String s = stringBuffer.toString();
        for(int i=0;i<KeyWordsConst.MyKeyWord.length;i++){
            if(s.contains(KeyWordsConst.MyKeyWord[i]))
                return true;
        }
        return false;
    }

    //处理读取的map数据中过期的数据
    public LinkedHashMap<String, String> filterExpireData(LinkedHashMap<String, String> originMap){
        LinkedHashMap<String, String> tempMap =  new LinkedHashMap<String,String>();
        LocalDate now = LocalDate.now();

        for (String key : originMap.keySet()) {
            String time = key.substring(key.indexOf('[')+1,key.indexOf(']'));
            if(inTwoWeeks(time,now))
                tempMap.put(key,originMap.get(key));
        }
        return tempMap;
    }

    //是否为两周内以内
    public Boolean inTwoWeeks(String task,LocalDate currentDate){
        LocalDate oneMonthsAgo = currentDate.minus(Period.ofDays(14));
        LocalDate parse = LocalDate.parse(task, DateTimeFormatter.ISO_LOCAL_DATE);
        if(parse.isBefore(oneMonthsAgo)){
            return false;
        }
        return true;
    }

    //出错时判断是否存在后缀 不存在跳过此条
    private Boolean lastExist(String title){
        String substring = title.substring(title.lastIndexOf('[') + 1, title.lastIndexOf(']'));
        return substring.isEmpty()?true:false;
    }

}
